package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ExtHelper;
import com.ruoyi.common.utils.uuid.IdGeneratorSnowflake;
import com.ruoyi.system.domain.TrainingCadetRotationPlan;
import com.ruoyi.system.mapper.TrainingCadetRotationPlanMapper;
import com.ruoyi.system.service.ITrainingCadetRotationPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 培训对象科室轮转计划Service业务层处理
 *
 * @author dp
 * @date 2023-11-27
 */
@Service
public class TrainingCadetRotationPlanServiceImpl implements ITrainingCadetRotationPlanService {
    @Autowired
    private TrainingCadetRotationPlanMapper trainingCadetRotationPlanMapper;
    @Autowired
    IdGeneratorSnowflake idGeneratorSnowflake;

    /**
     * 查询培训对象科室轮转计划
     *
     * @param rotationPlanId 培训对象科室轮转计划主键
     * @return 培训对象科室轮转计划
     */
    @Override
    public TrainingCadetRotationPlan selectTrainingCadetRotationPlanByRotationPlanId(Long rotationPlanId) {
        return trainingCadetRotationPlanMapper.selectTrainingCadetRotationPlanByRotationPlanId(rotationPlanId);
    }

    /**
     * 查询培训对象科室轮转计划列表
     *
     * @param trainingCadetRotationPlan 培训对象科室轮转计划
     * @return 培训对象科室轮转计划
     */
    @Override
    public List<TrainingCadetRotationPlan> selectTrainingCadetRotationPlanList(TrainingCadetRotationPlan trainingCadetRotationPlan) {
        return trainingCadetRotationPlanMapper.selectTrainingCadetRotationPlanList(trainingCadetRotationPlan);
    }

    /**
     * 新增培训对象科室轮转计划
     *
     * @param trainingCadetRotationPlan 培训对象科室轮转计划
     * @return 结果
     */
    @Override
    public int insertTrainingCadetRotationPlan(TrainingCadetRotationPlan trainingCadetRotationPlan) {
        TrainingCadetRotationPlan trainingTemp = new TrainingCadetRotationPlan();
        //根据培训对象id和时间段查询数据是否重复
        trainingTemp.setCadetId(trainingCadetRotationPlan.getCadetId());
        trainingTemp.setStartTime(trainingCadetRotationPlan.getStartTime());
        trainingTemp.setEndTime(trainingCadetRotationPlan.getEndTime());
        List<TrainingCadetRotationPlan> trainingCadetRotationPlanList =
                trainingCadetRotationPlanMapper.selectTrainingCadetRotationPlanList(trainingTemp);
        if (!trainingCadetRotationPlanList.isEmpty()) {
            throw new RuntimeException("该学员同时段已有轮转计划！");
        }
        if (trainingCadetRotationPlan.getMentorId() != null && trainingCadetRotationPlan.getMentorId().isEmpty()) {
            trainingCadetRotationPlan.setMentorId(null);
        }
        return trainingCadetRotationPlanMapper.insertTrainingCadetRotationPlan(trainingCadetRotationPlan);
    }

    /**
     * 修改培训对象科室轮转计划
     *
     * @param trainingCadetRotationPlan 培训对象科室轮转计划
     * @return 结果
     */
    @Override
    public int updateTrainingCadetRotationPlan(TrainingCadetRotationPlan trainingCadetRotationPlan) {
        if (trainingCadetRotationPlan.getMentorId() != null && trainingCadetRotationPlan.getMentorId().isEmpty()) {
            trainingCadetRotationPlan.setMentorId(null);
        }
        return trainingCadetRotationPlanMapper.updateTrainingCadetRotationPlan(trainingCadetRotationPlan);
    }

    /**
     * 批量删除培训对象科室轮转计划
     *
     * @param rotationPlanIds 需要删除的培训对象科室轮转计划主键
     * @return 结果
     */
    @Override
    public int deleteTrainingCadetRotationPlanByRotationPlanIds(Long[] rotationPlanIds) {
        return trainingCadetRotationPlanMapper.deleteTrainingCadetRotationPlanByRotationPlanIds(rotationPlanIds);
    }

    /**
     * 删除培训对象科室轮转计划信息
     *
     * @param rotationPlanId 培训对象科室轮转计划主键
     * @return 结果
     */
    @Override
    public int deleteTrainingCadetRotationPlanByRotationPlanId(Long rotationPlanId) {
        return trainingCadetRotationPlanMapper.deleteTrainingCadetRotationPlanByRotationPlanId(rotationPlanId);
    }

    /**
     * 实体数据补全
     *
     * @param trainingCadetRotationPlan 实体
     * @param userId                    用户id
     * @param crateType                 创建类型 1：新增 2：修改
     */
    @Override
    public void completeData(TrainingCadetRotationPlan trainingCadetRotationPlan, Long userId, Integer crateType) {
        //主键
        if (trainingCadetRotationPlan.getRotationPlanId() == null || "".equals(trainingCadetRotationPlan.getRotationPlanId())) {
            trainingCadetRotationPlan.setRotationPlanId(ExtHelper.toString(idGeneratorSnowflake.nextId()));
        }
        //创建人、创建时间
        if (crateType == 1) {
            if (trainingCadetRotationPlan.getCreateBy() == null) {
                trainingCadetRotationPlan.setCreateBy(userId.toString());
            }
            if (trainingCadetRotationPlan.getCreateTime() == null) {
                trainingCadetRotationPlan.setCreateTime(DateUtils.getNowDate());
            }
        }
        //修改人
        if (trainingCadetRotationPlan.getModifyBy() == null) {
            trainingCadetRotationPlan.setModifyBy(userId);
        }
        //修改时间
        if (trainingCadetRotationPlan.getModifyTime() == null) {
            trainingCadetRotationPlan.setModifyTime(DateUtils.getNowDate());
        }
    }
}
